<?php

class RoleController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {

        $roleType = '';

        //Load ACL Model
        $aclModel = new Model_Acl();
        $resourcesData = $aclModel->getResources();
        $resources = Array();
        foreach ($resourcesData as $controller => $_resources) {
            if (count($_resources) > 0) {
                $resources[$controller] = $_resources;
            }
        }

        $roleArray = Array();
        $errorMessageArray = Array();
        $succesMessage = '';
        
        $this->view->resources = $resources;

        $roles = $aclModel->getRoles();
        $this->view->roles = $roles;


        //Load the Role Access Model and delete
        $roleAccessModel = new Model_Roleaccess();

        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();
            $roleType = $postedData['roles'];
            if ($roleType == '') {
                $errorMessageArray[] = "Please select role";
            }
            if (count($errorMessageArray) <= 0) {

                //Delete if Form Submitted
                if (array_key_exists('save', $postedData)) {
                    $roleAccessModel->delete("role_type = '" . $roleType . "'");
                }

                foreach ($postedData as $_key => $_data) {
                    //If the Submit is clicked
                    if (array_key_exists('save', $postedData)) {
                        $pos = strpos($_key, 'chk_');
                        if ($pos === false) {

                        } else {
                            //Over here
                            $explodeArray = explode("_", $_key);
                            if (count($explodeArray) > 2) {
                                $_controler = $explodeArray['1'];
                                $_action = $explodeArray['2'];
                                //Role Accesss Array
                                $roleAccessArray = Array();
                                $roleAccessArray['role_type'] = $roleType;
                                $roleAccessArray['ra_module'] = 'default';
                                $roleAccessArray['ra_controller'] = $_controler;
                                $roleAccessArray['ra_action'] = $_action;
                                $roleAccessModel->save($roleAccessArray, 'ra_id');
                                $succesMessage = "Role updated successfully";
                            }
                        }
                    }
                }
            }
        }

        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $succesMessage;

        //Load the roles given by Admin
        $roleAccess = $roleAccessModel->fetchEntryById($roleType);
        if (count($roleAccess) > 0) {
            foreach ($roleAccess as $_key => $_role) {
                $roleArray[$_role['ra_controller']][$_role['ra_action']] = true;
            }
        }
        $this->view->roleType = $roleType;
        $this->view->roleArray = $roleArray;
    }

}

?>
